using UnityEngine;
using System.Collections;

public class ConnectionGUI : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public void OnGUI()
	{
	
		if(Network.peerType == NetworkPeerType.Disconnected)
		{
		/* If not connected
		 **/ 
			if( GUI.Button(new Rect(10,10,100,30),"Connect") == true )
			{
				Network.Connect(RemoteIP,RemotePort);
				
			}
			if( GUI.Button(new Rect(10,50,100,30),"Start Server") == true )
			{
				/* CreateServer
				 * */
				Network.InitializeServer(32,ListenPort);
				
				/* Notify our objects that the level and the network is ready
				 * */
				foreach(GameObject GO in FindObjectsOfType(typeof(GameObject)) )
				{
					GO.SendMessage("OnNetworkLoadedLevel",SendMessageOptions.DontRequireReceiver);
				}
			}
			
			/*
			 * */
			RemoteIP = GUI.TextField(new Rect(120,10,100,20),RemoteIP);
			RemotePort = System.Convert.ToInt32(GUI.TextField(new Rect(120,100,100,20),RemotePort.ToString()));
			
			
			
		}
		else{
			string IPAddress = Network.player.ipAddress;
			string Port = Network.player.port.ToString();
			
			GUI.Label(new Rect(140,20,250,40),"IP Address:" + IPAddress+":"+Port);
			if(GUI.Button(new Rect(10,10,100,50),"Disconnected"))
			{
				Network.Disconnect(200);
			}
		}
		
		
	}
	
	void OnConnectedToServer(){
		foreach(GameObject GO 	in FindObjectsOfType(typeof(GameObject)))
		{
			GO.SendMessage("OnNetworkLoadedLevel",SendMessageOptions.DontRequireReceiver);
		}
	}
	
	
	public string RemoteIP = "127.0.0.1";
	public int RemotePort= 25000;
	public int ListenPort = 25000;
	public bool UseNAT = false;
	public string YourIP = "";
	public string YourPort = "";
	
}
